package com.dhxt.springcloud;
import java.nio.charset.StandardCharsets;
import java.util.ArrayList;
import java.util.List;

import org.springframework.boot.SpringApplication;
import org.springframework.cloud.client.SpringCloudApplication;
import org.springframework.cloud.client.loadbalancer.LoadBalanced;
import org.springframework.cloud.netflix.zuul.EnableZuulProxy;
import org.springframework.context.annotation.Bean;
import org.springframework.http.client.ClientHttpRequestInterceptor;
import org.springframework.http.client.support.BasicAuthorizationInterceptor;
import org.springframework.http.converter.HttpMessageConverter;
import org.springframework.http.converter.StringHttpMessageConverter;
import org.springframework.web.client.RestTemplate;
/**
 * 
 * @Description: 微服务网关,所有请求先进入网关, 然后通过bootstrap.properties 配置来调取对应的注册中心的微服务接口
 *               调用方式为 http://localhost:3004/springCloudTest/jpaController/test
 *               3004为zuul端口 
 *               springCloudTest:是注册中心application名称
 *               jpaController/test为微服务的接口名称
 * @author hp
 * @date 2017年7月14日 下午6:04:01
 */
@EnableZuulProxy
@SpringCloudApplication
public class ZuulApplication  {
	
	@Bean
	@LoadBalanced
	public RestTemplate restTemplate() {
		RestTemplate restTemplate = new RestTemplate();
		// 配置访问微服务账号密码
		List<ClientHttpRequestInterceptor> list = new ArrayList<ClientHttpRequestInterceptor>();
		list.add(new BasicAuthorizationInterceptor("hp", "hp"));
		restTemplate.setInterceptors(list);
		
		// 配置字符集
		List<HttpMessageConverter<?>> msgList = restTemplate.getMessageConverters();
		for (HttpMessageConverter<?> converter : msgList) {
			if (converter instanceof StringHttpMessageConverter) {
				((StringHttpMessageConverter) converter).setDefaultCharset(StandardCharsets.UTF_8);
				break;
			}
		}
		
		return restTemplate;
	}
	
    public static void main(String[] args) throws Exception {
        SpringApplication.run(ZuulApplication.class,args);
    }
    
}